package handler

import (
	"database-platform/handler/common"
	"database-platform/logger"
	"database-platform/model"
	"database-platform/service"
	"fmt"
	"github.com/gin-gonic/gin"
)

// ListInstanceHandler 处理获取实例列表的请求
func ListInstanceHandler(c *gin.Context) {
	params, err := getInstanceListParams(c)
	if err != nil {
		msg := fmt.Sprintf("ListInstanceHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	res, err := service.GetInstanceList(params)
	if err != nil {
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithData(c, res)
}

func getInstanceListParams(c *gin.Context) (params *model.InstanceListRequest, err error) {
	params = &model.InstanceListRequest{
		Hostname:    c.DefaultQuery("hostname", ""),
		IP:          c.DefaultQuery("ip", ""),
		Role:        c.DefaultQuery("role", ""),
		DBType:      c.DefaultQuery("db_type", ""),
		ClusterID:   common.ParseInt(c.DefaultQuery("cluster_id", "")),
		ShardNum:    common.ParseInt(c.DefaultQuery("shard_num", "")),
		DBVersion:   c.DefaultQuery("db_version", ""),
		DeployType:  c.DefaultQuery("deploy_type", ""),
		Env:         c.DefaultQuery("env", ""),
		Status:      c.DefaultQuery("status", ""),
		Search:      c.DefaultQuery("search", ""),
		ShowCluster: common.ParseBool(c.DefaultQuery("show_cluster", "false")),
		ShowServer:  common.ParseBool(c.DefaultQuery("show_server", "false")),
	}
	params.Page, params.PageSize = common.GetPagePageSizeParams(c)
	return params, err
}

func RetrieveInstanceHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("RetrieveInstanceHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}
	result, err := service.GetInstanceById(id)
	if err != nil {
		msg := fmt.Sprintf("RetrieveInstanceHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, fmt.Sprintf("获取详情错误，id:%v, err:%s", id, err.Error()))
		return
	}
	common.OkWithData(c, result)
}

// InsertInstanceHandler 处理插入实例记录的请求
func InsertInstanceHandler(c *gin.Context) {

	var instance model.Instance
	if err := c.ShouldBindJSON(&instance); err != nil {
		logger.Logger.Error(fmt.Sprintf("InsertInstanceHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	err := service.InsertInstance(&instance)
	if err != nil {
		msg := fmt.Sprintf("InsertInstanceHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "插入成功")
}

// UpdateInstanceHandler 处理更新实例记录的请求
func UpdateInstanceHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateInstanceHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}

	var instance model.Instance
	if err := c.ShouldBindJSON(&instance); err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateInstanceHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	err = service.UpdateInstance(id, &instance)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateInstanceHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "更新成功")
}

// DeleteInstanceHandler 处理删除实例记录的请求
func DeleteInstanceHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("DeleteInstanceHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}

	err = service.DeleteInstance(id)
	if err != nil {
		msg := fmt.Sprintf("DeleteInstanceHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "删除成功")
}
